package com.heima.article.controller.v1;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.heima.article.service.AIChatService;
import com.heima.article.service.ArticleClassifyService;
import com.heima.article.service.ChatRecordService;
import com.heima.model.article.dtos.ArticleQuestion;
import com.heima.model.article.dtos.ChatDto;
import com.heima.model.article.dtos.ChatRecordDto;
import com.heima.model.common.dtos.ResponseResult;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName AIChatController
 * @Description TODO
 * @Author Neo
 * @Date 2024/3/6 16:17
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v1/ai/")
public class AIChatController {


    @Autowired
    private AIChatService aiChatService;
    @Autowired
    private ArticleClassifyService articleClassifyService;
    @Autowired
    private ChatRecordService chatRecordService;

    @PostMapping("chat")
    public ResponseResult chat(@RequestBody ArticleQuestion question) {
        return aiChatService.chat(question.getContent());
    }
    @PostMapping("chatWithMessage")
    public ResponseResult chatWithMessage(@RequestBody ChatDto chatDto) {
        try {
            return aiChatService.chatWithMessage(chatDto);
        } catch (NoApiKeyException e) {
            throw new RuntimeException(e);
        } catch (InputRequiredException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/classify")
    public ResponseResult classify(@RequestBody ArticleQuestion question) {
        String res = articleClassifyService.classifyArticle(question.getArticleId(), question.getContent());
        return ResponseResult.okResult(res);
    }

    @PostMapping("/simplify")
    public ResponseResult simplify(@RequestBody ArticleQuestion question) {
        String res = articleClassifyService.simplifyArticle(question.getArticleId(), question.getContent());
        return ResponseResult.okResult(res);
    }
    @PostMapping("/chatRecord")
    public ResponseResult chatRecord(@RequestBody ChatRecordDto dto) {
        return chatRecordService.chatRecord(dto);
    }

    @PostMapping("/getChatRecord")
    public ResponseResult getChatRecord(@RequestBody ChatRecordDto dto) {
        return chatRecordService.getChatRecord(dto);
    }

}
